<?php
namespace App\Models\Trade\Supply;

use App\Models\Model;

class SupplyCollectionModel extends Model
{

    const TABLENAME = 'tw_trade_supply_collection';

    /**
     * 操作类型（添加收藏）
     */
    const ACTION_TYPE_DEFAULT = 1;
    
    /**
     * 操作类型（删除收藏）
     */
    const ACTION_TYPE_DELETE = 2;

    /**
     * 获取所有操作类型
     *
     * @return array
     */
    public static function getActionTypeList()
    {
        return array(
            self::ACTION_TYPE_DEFAULT,
            self::ACTION_TYPE_DELETE
        );
    }
    
    /**
     * 获取一条数据
     *
     * @param int $userId            
     * @param int $supplyId            
     * @return \Illuminate\Support\Collection
     */
    public static function findItem($userId, $supplyId)
    {
        $where[] = ['user_id', $userId];
        $where[] = ['supply_id', $supplyId];
        
        return self::dbTable()->where($where)->get();
    }

    /**
     * 添加一条数据
     *
     * @param int $userId            
     * @param int $supplyId            
     * @return false | id
     */
    public static function addItem($userId, $supplyId)
    {
        if (! $userId || ! $supplyId) {
            return false;
        }
        $data['user_id'] = $userId;
        $data['supply_id'] = $supplyId;
        $data['created'] = time();
        
        return self::dbTable()->insertGetId($data);
    }

    /**
     * 删除一条数据（物理删除）
     *
     * @param int $userId            
     * @param int $supplyId        
     * @return false | 1 | 0（删除条数，0未删除）
     */
    public static function banItem($userId, $supplyId)
    {
        if (! $userId || ! $supplyId) {
            return false;
        }
        $where[] = ['user_id', $userId];
        $where[] = ['supply_id', $supplyId];
        
        return self::dbTable()->where($where)->delete();
    }

    /**
     * 删除多条数据（物理删除）
     *
     * @param int $userId
     * @param array $supplyIds
     * @return false | 1 | 0（删除条数，0未删除）
     */
    public static function banList($userId, $supplyIds)
    {
        if (! $userId || ! $supplyIds || ! count($supplyIds)) {
            return false;
        }
        $where[] = ['user_id', $userId];
    
        return self::dbTable()->where($where)
            ->whereIn('supply_id', $supplyIds)
            ->delete();
    }
}
